package definite.Guide.Book.CoreSwingComponent.JButtonClass;

import java.awt.EventQueue;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;

import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JRootPane;

public class ActionButtonSample {

	public static void main(String[] args) {
		Runnable runner = new Runnable() {

			@Override
			public void run() {

				JFrame frame = new JFrame("Action Button Sample");
				frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

				ActionListener actionListener = new ActionListener() {

					@Override
					public void actionPerformed(ActionEvent e) {
						String actionCommand = e.getActionCommand();
						System.out.println("Selected : " + actionCommand);
					}
				};

				frame.setLayout(new GridLayout(2, 2, 10, 10));

				JButton button1 = new JButton("Text Button");
				button1.setMnemonic(KeyEvent.VK_B);
				button1.setActionCommand("First");
				button1.addActionListener(actionListener);

				Icon icon = new ImageIcon("play.gif");
				JButton button2 = new JButton(icon);
				button2.setActionCommand("Second");
				button2.addActionListener(actionListener);

				JButton button3 = new JButton("Playing", icon);
				button3.setActionCommand("Third");
				button3.addActionListener(actionListener);

				String htmlButton = "<html><sup>HTML</sup> <sub><em>Button</em></sub><br>"
						+ "<font color=\"#FF0080\"><u>Multi-line</u></font>";
				JButton button4 = new JButton(htmlButton);
				button4.setActionCommand("Fourth");
				button4.addActionListener(actionListener);

				frame.add(button1);
				frame.add(button2);
				frame.add(button3);
				frame.add(button4);
				for (int i = 0; i < 6; i++)
					frame.add(new JButton(Integer.toString(i)));

				JRootPane rootPane = frame.getRootPane();
				rootPane.setDefaultButton(button2);

				frame.setLocationRelativeTo(null);
				frame.setSize(300, 200);
				frame.setVisible(true);

			}
		};

		EventQueue.invokeLater(runner);
	}

}
